using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using T2.WebSecurity.Business;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.Business.Entities;

namespace T2.WebSecurity.Business.Workflow.Activites
{
	public sealed class Db_T2_WebSecurity_ApplicationUser_Remove : CodeActivity
	{
		private IBusinessService bs = null;
		public OutArgument<IDictionary<string, int>> RetVal { get; set; }
		public InArgument<IBusinessService> BS {get;set;}
		public InArgument<IApplicationUser_biz> RemoveParam { get; set; }
		private IApplicationUser_biz removeParam ;
        IDictionary<string, int> tempReturn;

		protected override void Execute(CodeActivityContext context)
		{
			bs = context.GetValue(BS);
            removeParam = context.GetValue(RemoveParam);
            if (removeParam != null)
            {
                removeParam.Action = "Remove";
                removeParam.ModifiedBy = removeParam.ModifiedBy;
                removeParam.ModifiedDt = removeParam.ModifiedDt;
                tempReturn = bs.ApplicationUserController_Set(removeParam);

                Int32 RA = GetValueFromDictionary("Rows_Affected", tempReturn);
                Int32 RV = GetValueFromDictionary("Return_Value", tempReturn);
                if (RA + RV == 0)
                {
                    throw new ApplicationException("You tried to overwrite the same record as another individual");
                }
                else
                {
                    context.SetValue(RetVal, tempReturn);
                }
            }
		}

        private Int32 GetValueFromDictionary(string Key, IDictionary<string, Int32> MyDictionary)
        {

            Int32 RetVal = MyDictionary.Where(z => z.Key == Key).Select(p => p.Value).FirstOrDefault();
            return RetVal;
        }
	}
}
